home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Tools - Objects / ResEdit / ResEdit 1.2 / Examples / CExamples / Source / ICONPick.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-04-04  |  6.4 KB  |  198 lines  |  [TEXT/MPS ]

  1. /*
  2.  COPYRIGHT (C) 1984-1989 Apple Computer,Inc.
  3.  All rights reserved
  4. */
  5.  
  6. /* Icon Resource Picker */
  7.  
  8. /* This is the Icon resource picker.  This picker is very similar to all other pickers.
  9.         The general scheme is that a List structure is created whose cells
  10.         contain the ID's of this resource type.  A drawproc is installed in the List
  11.         record which is called to display the resource.  Obviously, graphical resources
  12.         are drawn as is, but descriptions of other types can be displayed also (usually
  13.         in a one-dimensional list). */
  14.  
  15. #include    <types.h>
  16. #include    <memory.h>
  17. #include    <menus.h>
  18. #include    <resources.h>
  19. #include    <lists.h>
  20.  
  21. #include    "ResEd.h"
  22.  
  23. #define listCellSizeH             0x40
  24. #define listCellSizeV                0x40
  25. #define theScrollBar                15
  26.     
  27. /* Needs to be 2 wide so that I can always tell a 2d list from a normal text list. */
  28. #define minIconsPerRow             2
  29. #define ICONMinWindowWidth     (minIconsPerRow * listCellSizeH) + theScrollBar
  30. #define ICONMinWindowHeight listCellSizeV
  31.     
  32. #define ICONResourceSize         128
  33.  
  34.  
  35. /* Used only for editors. */
  36. pascal void EditBirth(Handle thing, ParentHandle dad)
  37. {
  38.     #pragma    unused (thing, dad)
  39. }
  40.  
  41. /* *********************************************************************************** */
  42.  
  43. /* Returns the width and the iconsPerRow. */
  44. short GetWidth (short *iconsPerRow)
  45. {
  46.     short width;
  47.  
  48.     width = PickStdWidth();
  49.     if (width > ICONMinWindowWidth) {
  50.         *iconsPerRow = (width - theScrollBar) / listCellSizeH;
  51.         width = (*iconsPerRow * listCellSizeH) + theScrollBar;
  52.         }
  53.     else {
  54.         width = ICONMinWindowWidth;
  55.         *iconsPerRow = minIconsPerRow;
  56.         }
  57.     return width;
  58. }
  59.  
  60. /* *********************************************************************************** */
  61.  
  62. short GetHeight (void)
  63. {
  64.     short height;
  65.     
  66.     height = PickStdRows() * DefaultListCellSize();
  67.     if (height > ICONMinWindowHeight) {
  68.         height = (height / listCellSizeV) * listCellSizeV;
  69.         }
  70.     else {
  71.         height = ICONMinWindowHeight;
  72.         }
  73.     return height;
  74. }
  75.  
  76. /* *********************************************************************************** */
  77.  
  78. pascal void PickBirth(ResType t, ParentHandle dad)
  79. {
  80.     PickHandle     pick;
  81.     PickPtr            p;                    /* temp ptr for *pick */
  82.     WindowPtr     myWindow;
  83.     Str255             from,
  84.                             myTitle;
  85.     Cell                cSize;
  86.     short                iconsPerRow;
  87.  
  88.     if (t != (ResType)'ICON') {
  89.         return;                                                                /* This only works for ICON resources.                         */
  90.     }
  91.         
  92.     GetStr(fromStr, miscStrings, &from);         /* Get ' from '                                                                     */
  93.     strcpy(myTitle, "\pICONs");
  94.     ConcatStr (myTitle, from);
  95.     myWindow = WindSetup(GetWidth (&iconsPerRow), GetHeight(), myTitle, (*dad)->name);
  96.     
  97.     if (myWindow != NULL)    {
  98.         /* Get the memory for my data structure. */
  99.         pick = (PickHandle)NewHandle(sizeof(PickRec));
  100.     
  101.         BubbleUp ((Handle)pick);                        /* Move it out of the way.                                                     */
  102.         HLock ((Handle)pick);                                /* Lock it down so that I can dereference it.             */
  103.     
  104.         p = *pick;
  105.  
  106.         p->father = dad;                             /* Back ptr to dad                                                                     */
  107.         memcpy(p->fName, (*dad)->name, (*dad)->name[0] + 1);    /* Add 1 for the length byte */
  108.         p->wind = myWindow;                     /* Directory window                                                                 */
  109.         p->rebuild = false;
  110.         p->pickID = ResEdID();                 /* Get my resource id                                                             */
  111.         p->rType = 'ICON';                         /* Resource type that I understand                                     */
  112.         p->rNum = CurResFile();             /* Owner res file                                                                     */
  113.         p->rSize = ICONResourceSize;
  114.     
  115.         ((WindowPeek)myWindow)->windowKind = ResEdID();    /* Get my resource ID                                         */
  116.         ((WindowPeek)myWindow)->refCon = (long)pick;    /* Save the data structure for later use.    */
  117.  
  118.         /* Create the list.                                                                                                                                         */
  119.         cSize.h = listCellSizeH;
  120.         cSize.v = listCellSizeV;
  121.         p->instances = WindList(myWindow, iconsPerRow, cSize, ResEdID());
  122.         
  123.         p->scroll = (*p->instances)->vScroll;
  124.         
  125.         LDoDraw(false, p->instances);    /* Don't draw it as it is built - too slow.                 */
  126.  
  127.         /* Fill in the list with the ids of the resources. */
  128.         p->nInsts = BuildType((ResType)'ICON', p->instances);
  129.         
  130.         /* Select the first icon. */
  131.         LSetSelect(true, *(Cell *)0, p->instances);
  132.         LDoDraw(true, p->instances);    /* Next time, draw them.                                                         */
  133.     
  134.         /* Need to update here to avoid the following:    The window is activated and the
  135.             user types a character which causes the list to scroll.  The activate event is processed
  136.             first and draws and hilights the first item.    The key down is processed next and scrolls
  137.             the window which has only one item drawn.  An update comes along which includes all but
  138.             the first item (which was updated by LActivate).    The display is left partially updated. */
  139.         BeginUpdate(myWindow);
  140.         LUpdate(myWindow->visRgn, p->instances);
  141.         EndUpdate(myWindow);
  142.     
  143.         HUnlock((Handle)pick);
  144.     }
  145. }
  146.  
  147. /* Everything except the grow box is taken care of for us by PickEvent. */
  148. pascal void DoEvent(EventRecord *evt, PickHandle pick)
  149. {
  150.     WindowPtr windPtr;
  151.     
  152.     /* Must do our own grow box manipulation so that the min size is set correctly. */
  153.     if ((evt->what == mouseDown) && (FindWindow(evt->where, &windPtr) == inGrow)) {
  154.         GrowMyWindow (ICONMinWindowWidth, ICONMinWindowHeight, windPtr, (*pick)->instances);
  155.         }
  156.     else {
  157.         PickEvent(evt, pick);
  158.         }
  159. }
  160.  
  161. /* Everything is taken care of for us by PickInfoUp */
  162. pascal void DoInfoUpdate(short oldID, short newID, PickHandle pick)
  163. {
  164.     PickInfoUp(oldID, newID, pick);
  165. }
  166.  
  167. pascal void DoMenu(short menu, short item, PickHandle pick)
  168. {
  169.      short         len, id;
  170.      Handle        tempH;
  171.      Cell            theCell;
  172.      
  173.     /* Since we loaded all of the icons to draw them, we need to release them when we close the window. */
  174.     if ((menu == fileMenu) && (item == closeItem))    {
  175.         BubbleUp((Handle)pick);
  176.         HLock((Handle)pick);
  177.                 
  178.         UseResFile((*pick)->rNum);                /* Make sure that we have the correct file.                     */
  179.         theCell.h = 0;                                        /* Start at the first cell.                                                     */
  180.         theCell.v = 0;
  181.         SetResLoad (false);                                /* Don't load them - we're trying to get rid of them! */
  182.         do    {                                                            /* Loop through all of the cells.                                         */
  183.             len = 2;                                                /* Get the id from the cell.                                                     */
  184.             LGetCell((Ptr)(&id), &len, theCell, (*pick)->instances);
  185.             if (len > 0)    {
  186.                 tempH = Get1Res ((ResType)'ICON', id);    /* Get the resource if it is loaded.                                     */
  187.                 if (tempH != NULL) {                        /* If not null then it was loaded.                                         */
  188.                     ReleaseResource (tempH);        /* Toss it if it isn't changed.                                             */
  189.                 }
  190.             }
  191.         } while (LNextCell (true, true, &theCell, (*pick)->instances));
  192.         SetResLoad (true);                                /* Always restore this!                                                                */
  193.     }
  194.     
  195.     /* Everything else is taken care of for us by PickMenu.                                                                     */
  196.     PickMenu(menu, item, pick);
  197. }
  198.